package com.laoye.controller;

import com.laoye.bean.Cart;
import com.laoye.mapper.CartMapper;
import com.laoye.service.CartService;
import com.laoye.util.Result;
import com.laoye.util.ThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartService cartService;
/**
 * 购物车中某一个商品数量
 */
    @GetMapping("getProductCount/{prod_id}")
    public Result<Integer> getCount(@PathVariable("prod_id") Integer prod_id){
        return Result.success("", cartService.getProductCount(prod_id));
    }

    /**
     * 获取购物车商品数量
     */
    @GetMapping("getCount")
    public Result<Integer> getCount(){
        return Result.success("", cartService.getCount());
    }
    @PostMapping("addCart")
    public Result<String> addToCart(@RequestBody Cart cart){
        cart.setUser_id(ThreadLocal.getCurrentId());
        cartService.addToCart( cart);
        return Result.success("成功添加到购物车");
    }

    @GetMapping("getCarts")
    public Result<List<Cart>> getCarts(){
        List<Cart> carts = cartService.getCart();
        return Result.success("", carts);
    }
    @DeleteMapping("deleteCart/{id}")
    public Result<String> deleteCart(@PathVariable("id") Integer prod_id){
        cartService.deleteCart(prod_id);
        return Result.success("订单商品删除成功");
    }
    @PutMapping("increaseCount/{id}")
    public Result<String> increaseCount(@PathVariable("id") Integer prod_id){
        cartService.increaseCount(prod_id);
        return Result.success("商品数量+1");
    }
    @PutMapping("decreaseCount/{id}")
    public Result<String> decreaseCount(@PathVariable("id") Integer prod_id){
        cartService.decreaseCount(prod_id);
        return Result.success("商品数量-1");
    }
    @PutMapping("updateCount")
    public Result<String> updateCount(@RequestParam("count") Integer count, @RequestParam("prod_id") Integer prod_id){
        cartService.updateCount(count, prod_id);
        return Result.success("商品数量更新成功");
    }
}
